#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "thread_alarm.h"
#include "thread_comm.h"
#include "hi_alarm.h"

/*************************************************************************/

#define ALARM_DBG     1
#define ALARM_ERR     1

#define ALARM_MODULE_TAG  " [ALARM]"

#if ALARM_DBG
#define ALARM_DBG_MSG(format, arg...) \
    do  \
    {   \
        printf("%s""<%d>===> "format, ALARM_MODULE_TAG,__LINE__,##arg);  \
    }   \
    while (0);
#else
#define ALARM_DBG_MSG(format, arg...)
#endif


#if ALARM_ERR
#define ALARM_ERR_MSG(format, arg...) \
    do  \
    {   \
        printf("%s""<%d>===> "format, ALARM_MODULE_TAG,__LINE__,##arg);  \
    }   \
    while (0);
#else
#define ALARM_ERR_MSG(format, arg...)
#endif

/*************************************************************************/

static int msgquequeid;
static int alarmfd;
static 	bool status_thread_alarm = FALSE;
/*************************************************************************/

void set_status_thread_alarm(bool status);
bool get_status_thread_alarm(void);

static EN_ERROR_CODE alarm_MsgReport( unsigned int minor )
{
    ST_MSG msg;
	EN_ERROR_CODE err;

	msg.type = MSG_T_MM_ALARM;
	msg.p1 = MSG_REPORT_GEN(MSG_T_ALARM,minor);
	
    err = msgsnd(msgquequeid, (void *)&msg, MSG_CONT_LEN, IPC_NOWAIT);
    if ( err < 0 )
    {
        ALARM_ERR_MSG("msgsnd \n");
        return EN_ERROR_CODE_SYSCALL;
    }
    return EN_ERROR_CODE_SUCCESS;
}

static EN_ERROR_CODE alarm_msg_proc(ST_MSG msg)
{   
    int ret;
    EN_ERROR_CODE err = EN_ERROR_CODE_SUCCESS;
    
	
	if (1 != syscfg.base_setting.alarm_setting.is_use_alarm)
	{
		ALARM_ERR_MSG("is_use_alarm 0\n");
		return EN_ERROR_CODE_FAIL;
	}  


    if (MSG_T_ALARM != msg.type)
    {
        ALARM_ERR_MSG("\n");
        return EN_ERROR_CODE_MSG_TYPE_FAIL;
    }  

	
    switch(msg.p1)
    {
        case 1://on
            ret = ioctl(alarmfd, GPIO_IOC_DATA, 1);
            if ( ret != 0 )
            {
                ALARM_ERR_MSG("ioctl failed\n");
                err = EN_ERROR_CODE_SYSCALL;
            }
            set_status_thread_alarm(TRUE);
            alarm_MsgReport(msg.p1);
            break;

        case 0://off 
            ret = ioctl(alarmfd, GPIO_IOC_DATA, 0);
            if (ret != 0)
            {
                ALARM_ERR_MSG("ioctl failed\n");
                err = EN_ERROR_CODE_SYSCALL;
            }
            set_status_thread_alarm(FALSE);
            alarm_MsgReport(msg.p1);
            break;

        default:    
            err = EN_ERROR_CODE_FAIL;
            break;
    }

    return err;
}


void set_status_thread_alarm(bool status)
{
	status_thread_alarm = status;
}

bool get_status_thread_alarm(void)
{

	return status_thread_alarm;
}


void *thread_alarm(void *arg)
{
	int ret;
	EN_ERROR_CODE err = EN_ERROR_CODE_SUCCESS;
    ST_MSG msg;

	msgquequeid = *((int *)arg);

    alarmfd = open("/dev/"HIALARM_DEVICE_NAME, O_RDWR);
    if ( alarmfd < 0 )
    {
    	set_status_thread_alarm(FALSE);
        ALARM_ERR_MSG("open alarm device error\n");
    }

    ALARM_DBG_MSG("open alarm device success\n");

    while ( 1 )
    {
        ret = msgrcv(msgquequeid, (void*)&msg, MSG_CONT_LEN, MSG_T_ALARM, 0);
        if ( ret < 0 )
        {
			ALARM_ERR_MSG("thread_camera msgrcv fail\n");
        }
		alarm_msg_proc(msg);
    }


	close(alarmfd);
    return ( ( void * )EN_ERROR_CODE_SUCCESS ); 
}

